package avro;


import example.avro.User;
import example.proto.Mail;
import example.proto.Message;
import org.apache.avro.file.DataFileReader;
import org.apache.avro.file.DataFileWriter;
import org.apache.avro.io.DatumReader;
import org.apache.avro.io.DatumWriter;
import org.apache.avro.ipc.NettyServer;
import org.apache.avro.ipc.NettyTransceiver;
import org.apache.avro.ipc.Server;
import org.apache.avro.ipc.specific.SpecificRequestor;
import org.apache.avro.ipc.specific.SpecificResponder;
import org.apache.avro.specific.SpecificDatumReader;
import org.apache.avro.specific.SpecificDatumWriter;
import org.apache.avro.util.Utf8;

import java.io.File;
import java.io.IOException;
import java.net.InetSocketAddress;

/**
 * Created with IntelliJ IDEA.
 * MyUser: belict
 * Date: 11/30/13
 * Time: 8:17 PM
 * To change this template use File | Settings | File Templates.
 */
public class Run {
    public static class MailImpl implements Mail {
        // in this simple example just return details of the message
        public Utf8 send(Message message) {
            System.out.println("Sending message");
            return new Utf8("Sending message to " + message.getTo().toString()
                    + " from " + message.getFrom().toString()
                    + " with body " + message.getBody().toString());
        }
    }

    private static Server server;

    private static void startServer() throws IOException {
        server = new NettyServer(new SpecificResponder(Mail.class, new MailImpl()), new InetSocketAddress(65111));
        // the server implements the Mail protocol (MailImpl)
    }

    public static void main(String[] args) throws IOException {
        if (args.length != 3) {
            System.out.println("Usage: <to> <from> <body>");
            System.exit(1);
        }

        System.out.println("Starting server");
        // usually this would be another app, but for simplicity
        startServer();
        System.out.println("Server started");

        NettyTransceiver client = new NettyTransceiver(new InetSocketAddress(65111));
        // client code - attach to the server and send a message
        Mail proxy = (Mail) SpecificRequestor.getClient(Mail.class, client);
        System.out.println("Client built, got proxy");

        // fill in the Message record and send it
        Message message = new Message();
        message.setTo(new Utf8(args[0]));
        message.setFrom(new Utf8(args[1]));
        message.setBody(new Utf8(args[2]));
        System.out.println("Calling proxy.send with message:  " + message.toString());
        System.out.println("Result: " + proxy.send(message));

        // cleanup
        client.close();
        server.close();
    }

    private static void saveInAvroJsonFile() throws IOException {
        User user1 = new User("alex",13,"blue");
        User user2 = new User("Jon",31,"Json");
        User user3 = User.newBuilder()
                .setName("Yuriy")
                .setFavoriteColor("red")
                .setFavoriteNumber(71)
                .build();

        File file = new File("/home/belict/hamsterStudy/src/main/resources/users.avro");

        DatumWriter<User> datumWriter = new SpecificDatumWriter<User>(User.class);
        DataFileWriter<User> fileWriter = new DataFileWriter<User>(datumWriter);
        fileWriter.create(user1.getSchema(),file);
        fileWriter.append(user1);
        fileWriter.append(user2);
        fileWriter.append(user3);
        fileWriter.close();
    }

    private static void deserealize() throws IOException {
        File file = new File("/home/belict/hamsterStudy/src/main/resources/users.avro");
        DatumReader<User> userDatumReader = new SpecificDatumReader<User>(User.class);
        DataFileReader<User> dataFileReader = new DataFileReader<User>(file,userDatumReader);
        User user = null;
        while(dataFileReader.hasNext()){
            user = dataFileReader.next();
            System.out.println(user);
        }
    }
}
